using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using QFramework;

namespace QFramework.Monk
{
	public partial class GetAllExp : PowerUp
	{
		static IEnumerator FlyToPlayer()
		{
			IEnumerable<PowerUp> expArray = FindObjectsByType<Exp>(FindObjectsInactive.Exclude, FindObjectsSortMode.None);
			IEnumerable<PowerUp> coinArray = FindObjectsByType<Gold>(FindObjectsInactive.Exclude, FindObjectsSortMode.None);
			expArray.Concat(coinArray);
			AudioKit.PlaySound("get_all_exp");
			foreach (var powerItem in expArray.OrderByDescending(up =>up.mIsInScreen))
			{
				if (!powerItem.mIsInScreen)
				{
					yield return new WaitForEndOfFrame();
				}
				ActionKit.OnUpdate.Register(() =>
				{
					var player = Player.Default;
					if (player)
					{
						var dir = (player.transform.position - powerItem.transform.position).normalized;
						powerItem.transform.Translate(dir*Time.deltaTime * 10f);
					}

				}).UnRegisterWhenGameObjectDestroyed(powerItem.gameObject);
			}
			
		}

		protected override void Execute()
		{
			GeneratorManager.Default.StartCoroutine(FlyToPlayer());
			this.gameObject.DestroySelfGracefully();
		}
		
	}
}
